﻿using System.Configuration;
using System.IO;
using System.Text.RegularExpressions;
using Pool.WebSite.Service.Adapter;

namespace Pool.WebSite.Service.Models
{
    class AttachmentDto : AbstractDto<FileInfo>
    {
        public AttachmentDto(FileInfo file) : base(file) { }

        public override dynamic ToDto()
        {
            string limitImageExtension = new Regex(string.Format(AttachmentAdapter.FILE_EXTENSION_REGEX_FORMAT, "image"))
                .Match(ConfigurationManager.AppSettings[AttachmentAdapter.EXNTESION]).Groups[AttachmentAdapter.REGEX_EXTENSION].Value;

            return new
            {
                is_dir = false,
                has_file = false,
                filesize = Entity.Length,
                is_photo = -1 < limitImageExtension.IndexOf(Entity.Extension),
                filetype = Entity.Extension.Substring(1),
                filename = Entity.Name,
                datetime = Entity.LastWriteTime.ToString("yyyy-MM-dd HH:mm:ss")
            };
        }
    }
}
